package dao

import (
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
	"time"
)

type Admin struct {
	Id        int       `json:"id" gorm:"primary_key" description:"自增主键"`
	UserName  string    `json:"user_name" gorm:"column:user_name" description:"用户名"`
	Salt      string    `json:"salt" gorm:"column:salt" description:"密码盐值"`
	Password  string    `json:"password" gorm:"column:password" description:"密码"`
	UpdatedAt time.Time `json:"updated_at" gorm:"column:update_at" description:"更新时间"`
	CreatedAt time.Time `json:"created_at" gorm:"column:create_at" description:"创建时间"`
	IsDelete  int       `json:"is_delete" gorm:"column:is_delete" description:"是否删除"`
}

func (a *Admin) TableName() string {
	return "gateway_admin"
}

func (a *Admin) Find(c *gin.Context, tx *gorm.DB, search *Admin) (*Admin, error) {
	admin := &Admin{}
	// 根据结构体查询
	err := tx.WithContext(c).Where(search).Find(admin).Error
	if err != nil {
		return nil, err
	}
	return admin, nil
}

// 更新用户信息
func (a *Admin) Save(c *gin.Context, tx *gorm.DB) error {
	// 更新用户信息
	err := tx.WithContext(c).Save(a).Error
	return err
}
